#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <openssl/md5.h>
#include <libmemcached/memcached.h>


char *upload(char *data, long size, char *server)
{
	int i, j, n;
	char *sName;
	unsigned char *key, *key1;
	memcached_st *memc;
	memcached_return rc;

	key = (char *) malloc(16);
	key1 = (char *) malloc(sizeof(char) * 250);
	sName = (char *) malloc(sizeof(char) *(strlen(server) + 10));
	
	n = sprintf(sName, "--SERVER=%s", server);
	memc = memcached(sName, strlen(sName));
  //memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
	if(memc == NULL){
		memcached_free(memc);
		free(sName);
		return NULL;
	}

	key = MD5(data, size, NULL);
  for(i=0; i < 16; i++)
	  sprintf(key1 + 2*i, "%.2x", key[i]);
	rc = memcached_set(memc, (unsigned char*) key1, strlen(key1), data, size, (time_t) 0, (uint32_t) 0);

	if(rc != MEMCACHED_SUCCESS){
		memcached_free(memc);
		free(sName);
		return NULL;
	}
	memcached_free(memc);
	free(sName);
	return key1;
}
